package com.peakmain.wanandroid.loginmodule;

import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.peakmain.wanandroid.loginmodule.base.BaseLoginActivity;
import com.peakmain.wanandroid.loginmodule.constant.LoginConstant;
import com.peakmain.wanandroid.loginmodule.contract.RegisterContract;
import com.peakmain.wanandroid.loginmodule.presenter.RegisterPresenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author: peakmain
 * createdata：2019/7/10
 * mail: 2726449200@qq.com
 * desiption:
 */
@Route(path = "/login/RegisterActivity")
public class RegisterActivity extends BaseLoginActivity<RegisterPresenter> implements RegisterContract.View {

    @BindView(R2.id.account)
    EditText account;
    @BindView(R2.id.password)
    EditText password;
    @BindView(R2.id.repassword)
    EditText repassword;
    @BindView(R2.id.toolbar)
    Toolbar mToolBar;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initInjector() {
        ARouter.getInstance().inject(this);
        mActivityComponent.inject(this);
    }

    @Override
    protected void initView() {
        /*TextView textView = (TextView) mToolBar.getChildAt(0);
        textView.getLayoutParams().width = LinearLayout.LayoutParams.MATCH_PARENT;//填充父类
        textView.setGravity(Gravity.CENTER_HORIZONTAL);//水平居中*/
        mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void showRegisterSuccess() {
        ToastUtils.showShort("注册成功，正在跳转首页。。。");
        SPUtils.getInstance().put(LoginConstant.ACCOUNT, account.getText().toString().trim());
        SPUtils.getInstance().put(LoginConstant.PASSWORD, password.getText().toString().trim());
        ARouter.getInstance().build("/activity/MainActivity")
                .navigation();
        finish();
    }

    @OnClick({R2.id.regitster})
    public void onClick(View view) {
        if (view.getId() == R.id.regitster) {
            String accountTxt = account.getText().toString();
            String passwordTxt = password.getText().toString();
            String repasswordTxt = repassword.getText().toString();
            if (!TextUtils.isEmpty(accountTxt)
                    && !TextUtils.isEmpty(passwordTxt)
                    && !TextUtils.isEmpty(repasswordTxt)) {
                if (passwordTxt.length() < 6) {
                    ToastUtils.showShort("输入密码不能小于6位数");
                } else {
                    if (passwordTxt.equals(repasswordTxt)) {
                        mPresenter.register(accountTxt, passwordTxt, repasswordTxt);
                    } else {
                        ToastUtils.showShort("两次密码不一致");
                    }
                }
            } else {
                ToastUtils.showShort("输入框不能为空");
            }


        }

    }
}
